<template>
	<block>
		<scroll-view scroll-y class="scrollPage">
			<cu-custom :isBack="false"></cu-custom>
			<view class="flex flex-wrap padding-lr-xl padding-bottom-sm">
				<view class="basis-xl">
					<view style="font-size: 90rpx; letter-spacing: 10rpx" class="margin-bottom-lg">
						<text class="text-black text-bold">{{ weekArrCs[nowWeek] }}</text>
					</view>
					<view>
						<text class="text-grey text-lg">{{ dateToString }}</text>
					</view>
				</view>
			</view>

			<view class="padding-lr-xl text-lg">
				<view v-for="(item0, index0) in kebiao" :key="item0.key0">
					<view v-if="nowWeek >= item1.startWeek && nowWeek <= item1.endWeek"
						v-for="(item1, index1) in kebiao[index0]" :key="item1.key1">
						<text class="text-black pre">{{ item1.startClass }} - {{ item1.startClass + item1.length - 1 }} 节 {{ item1.name }} - {{ item1.where }}</text>
					</view>
				</view>
			</view>
			<view v-if="cengKeKeBiaoShow" class="padding-lr-xl text-lg">
				<view class="flex justify-between align-center">
					<view class="padding-tb-sm">Ta 的课：</view>
				</view>

				<view v-for="(item0, index0) in cengKeKeBiao" :key="item0.key0">
					<view v-if="nowWeek >= item1.startWeek && nowWeek <= item1.endWeek"
						v-for="(item1, index1) in cengKeKeBiao[index0]" :key="item1.key1">
						<text class="text-black pre">{{ item1.startClass }} - {{ item1.startClass + item1.length - 1 }} 节 {{ item1.name }} - {{ item1.where }}</text>
					</view>
				</view>
			</view>

			<view class="margin-lg margin-bottom-xl padding-sm bg-white radius shadow shadow-lg">
				<view class="flex-sub text-center">
					<view @tap="copyLizhi" class="flex text-df text-left padding-sm justify-start">
						<text>{{ lizhiArray[1] }}</text>
					</view>
					<view class="flex text-df padding-sm justify-end">—— {{ lizhiArray[2] }}</view>
					<view class="flex text-df justify-end">
						<view>
							<button :class="'cu-btn line-black round icon ' + (lizhiIsLoad ? 'loading' : '')"
								@tap="updateLizhi">
								<text class="cuIcon-loading"></text>
							</button>
						</view>
						<button style="margin-right: 30rpx; margin-left: 30rpx"
							:class="'cu-btn icon ' + lizhiClass + ' round animation-' + (animation == 'shake' ? 'shake' : '')"
							:data-class="'shake'" @tap="lizhiButton">
							<text :class="'cuIcon-likefill ' + lizhiText"></text>
						</button>
					</view>
				</view>
			</view>

			<view class="margin-lg margin-top-xl padding-sm bg-white radius shadow shadow-lg">
				<view class="cu-bar">
					<view class="action text-lg">
						<text class="cuIcon-favorfill text-orange"></text>
						常用
					</view>
					<view class="action">
						<button :class="'cu-btn line-black icon round ' + settingIcon" @tap="showSetting"
							data-target="gridModal">
							<text class="cuIcon-settings"></text>
						</button>
					</view>
				</view>
				<view class="cu-list grid col-4 no-border">
					<view class="cu-item" v-if="item.choose" :data-to="'/pages/' + item.to + '/index'" @tap="target"
						v-for="(item, index) in favList" :key="index">
						<view :class="'cuIcon-' + item.icon + ' text-' + item.color"></view>

						<text>{{ item.name }}</text>
					</view>
				</view>
			</view>

			<view :class="'cu-modal drawer-modal justify-end ' + (setting == 'show' ? 'show' : '')" @tap="hideSetting">
				<view class="cu-dialog basis-lg" catchtap :style="'padding-top:' + CustomBar + 'px'">
					<view class="cu-list menu text-left padding-top-lg">
						<view :data-choose="item.choose" :data-id="index" @tap="changefav" class="cu-item"
							style="height: 50rpx" v-for="(item, index) in favList" :key="item.key">
							<view class="content cu-form-group">
								<view class="text-df">{{ item.name }}</view>
								<switch class="cyan sm" :checked="item.choose"></switch>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="margin-lg margin-top-xl padding-sm bg-white radius shadow shadow-lg">
				<view class="cu-bar">
					<view class="action text-lg">
						<text class="cuIcon-list text-pink"></text>
						全部功能
					</view>
				</view>

				<view class="cu-list grid col-4 no-border">
					<view class="cu-item" data-to="/pages/kebiao/index" @tap="target">
						<view class="cuIcon-formfill text-red"></view>
						<text>课表</text>
					</view>
					<view class="cu-item" data-to="/pages/kong/index" @tap="target">
						<view class="cuIcon-list text-blue"></view>
						<text>空教室查询</text>
					</view>
					<view class="cu-item" data-to="/pages/linian/index" @tap="target">
						<view class="cuIcon-rankfill text-purple"></view>
						<text>历年成绩</text>
					</view>
					<view class="cu-item" data-to="/pages/bujige/index" @tap="target">
						<view class="cuIcon-roundcheckfill text-mauve"></view>
						<text>不及格成绩</text>
					</view>
					<view class="cu-item" data-to="/pages/xiaoli/index" @tap="target">
						<view class="cuIcon-activity text-brown"></view>
						<text>校历</text>
					</view>
					<!-- <view class="cu-item" data-to="/pages/xieyi/index" @tap="target">
						<view class="cuIcon-edit text-pink"></view>
						<text>用户协议</text>
					</view> -->
					<view class="cu-item" data-to="/pages/putonghua/index" @tap="target">
						<view class="cuIcon-voice text-green"></view>
						<text>普通话查询</text>
					</view>
					<view class="cu-item" data-to="/pages/cengke/index" @tap="target">
						<view class="cuIcon-similar text-orange"></view>
						<text>蹭课</text>
					</view>
				</view>
			</view>
			<view class="margin-lg padding-sm bg-white radius shadow shadow-lg">
				<view class="cu-bar">
					<view class="action text-lg">
						<text class="cuIcon-lock text-black"></text>
						未完成功能
					</view>
				</view>
				<view class="cu-list grid col-4 no-border wwc">
					<view class="cu-item" data-to="/pages/kaoshi/index" @tap="target">
						<view class="wwc-grey cuIcon-sort text-cyan"></view>
						<text>考试安排</text>
					</view>
				</view>
			</view>

			<view class="cu-tabbar-height"></view>
		</scroll-view>
	</block>
</template>

<script>
	// pages/shouye.js
	const app = getApp();
	export default {
		data() {
			return {
				StatusBar: app.globalData.StatusBar,
				CustomBar: app.globalData.CustomBar,
				lizhiArray: app.globalData.lizhiArray,
				PageCur: 'function',
				week: '',
				dateToString: new Date().toDateString(),
				nowDay: new Date().getDay() == 0 ? 6 : new Date().getDay() - 1,
				lizhiClass: 'line-black',
				lizhiArray: [],
				lizhiText: '',
                lizhiLike: 0,
				weekArrCs: [
					'未开始',
					'第一周',
					'第二周',
					'第三周',
					'第四周',
					'第五周',
					'第六周',
					'第七周',
					'第八周',
					'第九周',
					'第十周',
					'第十一周',
					'第十二周',
					'第十三周',
					'第十四周',
					'第十五周',
					'第十六周',
					'第十七周',
					'第十八周',
					'第十九周',
					'第二十周',
					'第二十一周',
					'第二十二周',
					'第二十三周',
					'第二十四周',
					'第二十五周',
					'假期中'
				],
				kebiao: [],
				cengKeKeBiao: [],
				cengKeKeBiaoShow: true,
				favList: [{
						icon: 'formfill',
						color: 'red',
						name: '课表',
						to: 'kebiao',
						choose: true
					},
					{
						icon: 'rankfill',
						color: 'purple',
						name: '及格成绩',
						to: 'linian',
						choose: true
					},
					{
						icon: 'list',
						color: 'blue',
						name: '空教室查询',
						to: 'kong',
						choose: true
					},
					// {
					// 	icon: 'edit',
					// 	color: 'pink',
					// 	name: '用户协议',
					// 	to: 'xieyi',
					// 	choose: false
					// },
					{
						icon: 'sort',
						color: 'cyan',
						name: '考试安排',
						to: 'kaoshi',
						choose: false
					},
					{
						icon: 'roundcheckfill',
						color: 'mauve',
						name: '不及格成绩',
						to: 'bujige',
						choose: false
					},
					{
						icon: 'activity',
						color: 'brown',
						name: '校历',
						to: 'xiaoli',
						choose: false
					},
					{
						icon: 'voice',
						color: 'green',
						name: '普通话查询',
						to: 'putonghua',
						choose: false
					},
					{
						icon: 'similar',
						color: 'orange',
						name: '蹭课',
						to: 'cengke',
						choose: true
					},
				],
				lizhiIsLoad: false,
				settingIcon: '',
				setting: '',
				animation: '',
				nowWeek: ''
			};
		},
		options: {
			addGlobalClass: true
		},
		beforeMount: function() {
			let that = this;
			var day = new Date();
			day.setTime(day.getTime());
			var s = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate();
			uni.getStorage({
				key: 'lizhiArray',
				success(res) {
					if (res.data[0] != s) {
						that.updateLizhi();
					} else {
						that.setData({
							lizhiArray: uni.getStorageSync('lizhiArray')
						});
					}
				},
				fail() {
					that.updateLizhi();
				}
			});
		},
		mounted() {
			let that = this;
			uni.getStorage({
				key: 'indexFavList',
				success: function(res) {
					for (var i = 0; i < res.data.length; i++) {
						if (that.favList[i].name == res.data[i].name) {
							that.favList[i].choose = res.data[i].choose;
						}
					}
					that.setData({
						favList: that.favList
					});
				}
			});
		},
		methods: {
			copyLizhi() {
				uni.setClipboardData({
					data: this.lizhiArray[1] + ' —— ' + this.lizhiArray[2],
					success() {}
				});
			},

			updateLizhi() {
				uni.showLoading({
					title: '正在获取'
				});
				this.setData({
					lizhiIsLoad: true
				});
				let that = this;
				var day = new Date();
				day.setTime(day.getTime());
				var s = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate();
				uni.request({
					url: 'https://v1.hitokoto.cn/?encode=json',
					success(res) {
						console.log(res)
						let content = res.data
						if (content.from === '原创' || content.from === '自己')
							content.from = 'Hitokoto'
						let lizhiArray = [s, content.hitokoto, content.from];
						uni.setStorage({
							data: lizhiArray,
							key: 'lizhiArray'
						});
						that.setData({
							lizhiArray
						});
						uni.hideLoading({
							complete: (res) => {}
						});
					},

					fail() {
						uni.hideLoading({
							complete: (res) => {}
						});
						uni.showToast({
							title: '获取失败',
							icon: 'none'
						});
					}
				});
				this.setData({
					lizhiIsLoad: false
				});
			},

			showSetting() {
				this.setData({
					settingIcon: 'setting1',
					setting: 'show'
				});
				setTimeout(() => {
					this.setData({
						settingIcon: ''
					});
				}, 500);
			},

			hideSetting(e) {
				this.setData({
					settingIcon: 'setting2',
					setting: ''
				});
				setTimeout(() => {
					this.setData({
						settingIcon: ''
					});
				}, 800);
			},

			target(e) {
				uni.navigateTo({
					url: e.currentTarget.dataset.to
				});
			},

			changefav(e) {
				var indexId = e.currentTarget.dataset.id;
				this.favList[indexId].choose = !this.favList[indexId].choose;
				this.setData({
					favList: this.favList
				});
				uni.setStorageSync('indexFavList', this.favList);
			},

			lizhiButton: function(e) {
				if (this.lizhiLike == 0) {
					this.setData({
						lizhiClass: 'line-pink',
						lizhiText: 'text-red',
						lizhiLike: 1
					});
					uni.setStorage({
						key: 'liked',
						data: 1
					});
				} else {
					this.setData({
						lizhiClass: 'line-black',
						lizhiText: '',
						lizhiLike: 0
					});
					uni.setStorage({
						key: 'liked',
						data: 0
					});
				}

				let that = this;
				that.setData({
					animation: 'shake'
				});
				setTimeout(function() {
					that.setData({
						animation: ''
					});
				}, 300);
			},

			getNewWeek() {
				let that = this;
				uni.getStorage({
					key: 'keBiaoDataWeekBegin',
					success: function(res) {
						let timeTemp = new Date().getTime();
						let weekTemp = Math.ceil((timeTemp - res.data) / 604800000);

						if (weekTemp > 26) {
							weekTemp = 26;
						}

						that.setData({
							nowWeek: weekTemp
						});
					},

					fail() {
						// that.getStart()
						that.setData({
							nowWeek: 0
						});
					},

					complete() {
						that.getDayClass();
					}
				});
			},

			getDayClass() {
				let that = this;
				let kebiao = uni.getStorageSync('keBiaoData');
				let cengKeKeBiao = uni.getStorageSync('cengKeKeBiaoData');
				let cengKeKeBiaoShow = uni.getStorageSync('cengKeSetIsShowHome');

				if (cengKeKeBiaoShow == '') {
					cengKeKeBiaoShow = false;
				}

				kebiao = kebiao[this.nowDay];
				cengKeKeBiao = cengKeKeBiao[this.nowDay];
				console.log(cengKeKeBiao)
				if (cengKeKeBiaoShow) {
					cengKeKeBiaoShow = false;

					for (let i = 0; i < 10; i++) {
						let dayClass = cengKeKeBiao[i];

						for (let j = 0; j < dayClass.length; j++) {
							let t = dayClass[j];
							let startWeek = t.startWeek;
							let endWeek = t.endWeek;

							if (this.nowWeek >= startWeek && this.nowWeek <= endWeek) {
								cengKeKeBiaoShow = true;
								break;
							}
						}

						if (cengKeKeBiaoShow) {
							break;
						}
					}
				}

				that.setData({
					kebiao: kebiao,
					cengKeKeBiao: cengKeKeBiao,
					cengKeKeBiaoShow: cengKeKeBiaoShow
				});
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>
